package cn.zn.modules.money.cur.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.modules.money.cur.dao.TMoneyInfoMapper;
import cn.zn.modules.money.cur.entity.TMoneyInfo;
import cn.zn.modules.money.cur.entity.vo.*;
import cn.zn.modules.money.cur.service.TMoneyInfoService;
import cn.zn.modules.money.enums.MoneyTypeEnum;
import cn.zn.modules.money.inter.MoneyInfoInter;
import cn.zn.modules.money.inter.vo.MoneyInfoVO;
import cn.zn.modules.pz.inter.values.PzConf;
import cn.zn.modules.pz.inter.values.PzConfBudgetCateItem;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TMoneyInfoServiceImpl extends ServiceImpl<TMoneyInfoMapper, TMoneyInfo> implements TMoneyInfoService, MoneyInfoInter {

    @Autowired
    private PzConf pzConf;

    @Override
    public List<MoneyInfoVO> findByIds(List<String> ids) {
        if (ids == null || ids.size() == 0) {
            return CollUtil.newArrayList();
        }
        return this.baseMapper.findList(ids).stream().map(o -> {
            MoneyInfoVO moneyInfoVO = new MoneyInfoVO();
            BeanUtils.copyProperties(o, moneyInfoVO);
            moneyInfoVO.setMoneyTypeName(MoneyTypeEnum.findByCode(o.getMoneyTypeCode()).getName());
            return moneyInfoVO;
        }).collect(Collectors.toList());
    }

    @Override
    public void updateFreeze(String projectCode, String cateCode, Double freezeMoney) {
        TMoneyInfo tMoneyInfo = super.getOne(new QueryWrapper<TMoneyInfo>()
                .eq("PROJECT_CODE_", projectCode)
                .eq("CATE_CODE_", cateCode));
        if (tMoneyInfo == null) {
            throw new RRException("预算不存在");
        }
        TMoneyInfo tMoneyInfoSave = new TMoneyInfo();
        tMoneyInfoSave.setId(tMoneyInfo.getId());
        tMoneyInfoSave.setFreezeMoney(NumberUtil.add(tMoneyInfo.getFreezeMoney(), freezeMoney));
        super.updateById(tMoneyInfoSave);
    }

    @Override
    public MoneyAvailableMoneyVO queryAvailableMoney(String projectCode, String cateCode) {
        String myCateCode = cateCode;
        Integer count = this.baseMapper.queryHasSmallCate(projectCode, cateCode);
        if (count == 0) {
            myCateCode = myCateCode.substring(0, 3);
        }
        return this.baseMapper.queryAvailableMoney(projectCode, myCateCode);
    }

    @Override
    public List<MoneyAvailableProjectVO> queryAvailableProjects() {
        return this.baseMapper.queryAvailableProjects();
    }

    @Override
    public List<MoneyAvailableCateVO> queryAvailableCates(String projectCode) {
        return this.baseMapper.queryAvailableCates(projectCode);
    }

    @Override
    public List<MoneySumVO> querySum() {
        return baseMapper.querySum();
    }

    @Override
    public List<TMoneyInfo> querySumDetail(TMoneyInfo tMoneyInfo) {
        return baseMapper.querySumDetail(tMoneyInfo);
    }

    @Override
    public List<TMoneyInfo> querySuspended(String id) {
        TMoneyInfo tMoneyInfo = super.getById(id);
        if (tMoneyInfo == null) {
            throw new RRException("经费不存在");
        }
        return baseMapper.querySuspended(tMoneyInfo);
    }

    @Override
    public void saveSuspended(TMoneyInfo tMoneyInfo) {
        // 查询经费
        TMoneyInfo moneyInfo = super.getById(tMoneyInfo.getId());
        if (moneyInfo == null) {
            throw new RRException("待暂停的经费不存在");
        }
        TMoneyInfo moneyInfoSuspended = new TMoneyInfo();
        BeanUtils.copyProperties(moneyInfo, moneyInfoSuspended);
        moneyInfoSuspended.setId(null);
        moneyInfoSuspended.setTotalMoney(NumberUtil.sub(Double.valueOf(0), tMoneyInfo.getTotalMoney()));
        super.save(moneyInfoSuspended);
    }

    @Override
    public List<MoneyCateMainNameSumVO> querySumForCateMainAll() {
        Map<String, MoneyCateMainSumVO> setupMap = this.baseMapper.querySumForCateMain().stream().collect(Collectors.toMap(MoneyCateMainSumVO::getCateCodeMain, o -> o));
        // 获取所有大类
        return pzConf.getBudget().getCateMain().stream().map(o -> {
            MoneyCateMainNameSumVO vo = new MoneyCateMainNameSumVO();
            vo.setCateCodeMain(o.getCode());
            vo.setCateNameMain(o.getName());
            if (setupMap.containsKey(o.getCode())) {
                MoneyCateMainSumVO moneyCateMainSumVO = setupMap.get(o.getCode());
                vo.setTotalMoney(moneyCateMainSumVO.getTotalMoney());
                vo.setFreezeMoney(moneyCateMainSumVO.getFreezeMoney());
                vo.setUseMoney(moneyCateMainSumVO.getUseMoney());
            }
            return vo;
        }).collect(Collectors.toList());
    }
}
